package no.ntnu.item.taxisystemgui.test.component;


public class ComponentSM extends no.ntnu.item.arctis.runtime.IStateMachine {


    protected no.ntnu.item.arctis.runtime.Scheduler scheduler;
    protected java.lang.String _stateMachineID;
    private final static int s_2 = 2, s_1 = 1, s_4 = 4, s_3 = 3;
    private com.bitreactive.library.counter.counter.Counter _b008;
    private com.bitreactive.library.counter.counter.Counter _b006;
    private no.ntnu.item.arctis.library.sessions.constructor.Constructor _b007;
    private no.ntnu.item.taxisystemgui.taxidispatch.TaxiDispatch _b009;
    private no.ntnu.item.arctis.library.sessions.constructor.Constructor _b005;
    private static java.util.Hashtable _alias = new java.util.Hashtable();
    private static int sessionCount = 0;

    public ComponentSM(no.ntnu.item.arctis.runtime.Scheduler scheduler, java.lang.String sessionID, java.lang.String parentSessionID) {
        super(sessionID, parentSessionID);
        this.scheduler = scheduler;
        ++ sessionCount;
    }


    protected void registerAlias(java.lang.String alias, java.lang.String sessionID){
        if(alias==null) no.ntnu.item.arctis.runtime.Logger.getLogger().applicationError("Provided alias is null", null);
        if(sessionID==null) no.ntnu.item.arctis.runtime.Logger.getLogger().applicationError("Provided sessionID is null", null);
        if(_alias.containsKey(alias)) no.ntnu.item.arctis.runtime.Logger.getLogger().applicationError("A session with alias " + alias + " already exists.", null);
        _alias.put(alias, sessionID);
    }

    public static java.lang.String findSession(java.lang.String alias){
        if(alias==null) no.ntnu.item.arctis.runtime.Logger.getLogger().applicationError("Try to find session for null alias", null);
        String sessionID = (String) _alias.get(alias);
        if(sessionID==null) {
            if(!_alias.contains(alias)) {
        	       no.ntnu.item.arctis.runtime.Logger.getLogger().applicationError("Session map does not contain alias " + alias, null);
            } else {
                no.ntnu.item.arctis.runtime.Logger.getLogger().applicationError("Session map has a null entry for alias " + alias, null);
            }
        }
return sessionID;
    }

    public int getSessionCount(){
        return sessionCount;
    }

    public static java.util.Collection allSessions(){
        return _alias.values();
    }

    public int fireInitial(){
        _b005 = new no.ntnu.item.arctis.library.sessions.constructor.Constructor("", "", 3, 0);
        _b005.setBlockID("_b005", sessionID);
        _b009 = new no.ntnu.item.taxisystemgui.taxidispatch.TaxiDispatch();
        _b009.setBlockID("_b009", sessionID);
        _b007 = new no.ntnu.item.arctis.library.sessions.constructor.Constructor("", "", 3, 0);
        _b007.setBlockID("_b007", sessionID);
        _b006 = new com.bitreactive.library.counter.counter.Counter();
        _b006.setBlockID("_b006", sessionID);
        _b008 = new com.bitreactive.library.counter.counter.Counter();
        _b008.setBlockID("_b008", sessionID);
        _b005.init();
        _b006.counter = _b005.getInstances();
        scheduler.startTimer(0, "t0", this);
        _b007.init();
        _b008.counter = _b007.getInstances();
        scheduler.startTimer(0, "t1", this);
        _state = s_2;
        return CONSUME_SIGNAL;
    }

    public int fire(java.lang.Object _sender, java.lang.Object sessionID, java.lang.String _signalID, java.lang.Object _data){
        if(_signalID.equals("_c1_TaxiClient_status")) {
            return handleSignal__c1_TaxiClient_status(_sender, sessionID, _data);
        } else if(_signalID.equals("_c0_UserClient_request")) {
            return handleSignal__c0_UserClient_request(_sender, sessionID, _data);
        } else if(_signalID.equals("_c0_UserClient_finish")) {
            return handleSignal__c0_UserClient_finish(_sender, sessionID, _data);
        } else if(_signalID.equals("_c1_TaxiClient_finish")) {
            return handleSignal__c1_TaxiClient_finish(_sender, sessionID, _data);
        }
        return TRIGGER_UNKNOWN;
    }

    private int handleSignal__c1_TaxiClient_status(java.lang.Object _sender, java.lang.Object sessionID, java.lang.Object _data){
        if(_state == s_4 || _state == s_1 || _state == s_2 || _state == s_3) {
            no.ntnu.item.taxisystemgui.taxiclient.TaxiClient _Test_status = (no.ntnu.item.taxisystemgui.taxiclient.TaxiClient) _data;
            return CONSUME_SIGNAL;
        }
        return DISCARD_SIGNAL;
    }

    private int handleSignal__c0_UserClient_request(java.lang.Object _sender, java.lang.Object sessionID, java.lang.Object _data){
        if(_state == s_3 || _state == s_2 || _state == s_4 || _state == s_1) {
            no.ntnu.item.taxisystemgui.userclient.UserClient _Test_request = (no.ntnu.item.taxisystemgui.userclient.UserClient) _data;
            return CONSUME_SIGNAL;
        }
        return DISCARD_SIGNAL;
    }

    private int handleSignal__c0_UserClient_finish(java.lang.Object _sender, java.lang.Object sessionID, java.lang.Object _data){
        if(_state == s_2 || _state == s_1 || _state == s_4 || _state == s_3) {
            return CONSUME_SIGNAL;
        }
        return DISCARD_SIGNAL;
    }

    private int handleSignal__c1_TaxiClient_finish(java.lang.Object _sender, java.lang.Object sessionID, java.lang.Object _data){
        if(_state == s_2 || _state == s_3 || _state == s_4 || _state == s_1) {
            return CONSUME_SIGNAL;
        }
        return DISCARD_SIGNAL;
    }

    public int fireTimer(java.lang.String _timerID){
        if(_timerID.equals("t3")) {
            return handleTimer_t3();
        } else if(_timerID.equals("t2")) {
            return handleTimer_t2();
        } else if(_timerID.equals("t1")) {
            return handleTimer_t1();
        } else if(_timerID.equals("t0")) {
            return handleTimer_t0();
        }
        return DISCARD_SIGNAL;
    }

    private int handleTimer_t3(){
        if(_state == s_4) {
            _b006.decreaseCounter();
            if(_b006.counterLargeZero()==true) {
                scheduler.restartTimer(_b005.getDelay(), "t3", this);
                java.lang.String _b005_o3 = _b005.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b005_o3);
                    scheduler.sendToSession(this, _sessionID, "_c0_UserClient_init", signalData);
                return CONSUME_SIGNAL;
            } else {
                _state = s_1;
                return CONSUME_SIGNAL;
            }
        } else if(_state == s_2) {
            _b006.decreaseCounter();
            if(_b006.counterLargeZero()==true) {
                scheduler.restartTimer(_b005.getDelay(), "t3", this);
                java.lang.String _b005_o3 = _b005.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b005_o3);
                    scheduler.sendToSession(this, _sessionID, "_c0_UserClient_init", signalData);
                return CONSUME_SIGNAL;
            } else {
                _state = s_3;
                return CONSUME_SIGNAL;
            }
        }
        return DISCARD_SIGNAL;
    }

    private int handleTimer_t2(){
        if(_state == s_2) {
            _b008.decreaseCounter();
            if(_b008.counterLargeZero()==true) {
                scheduler.restartTimer(_b007.getDelay(), "t2", this);
                java.lang.String _b007_o3 = _b007.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b007_o3);
                    scheduler.sendToSession(this, _sessionID, "_c1_TaxiClient_init", signalData, no.ntnu.item.taxisystemgui.taxiclient.TaxiClient.getAlias(_b007_o3));
                return CONSUME_SIGNAL;
            } else {
                _state = s_4;
                return CONSUME_SIGNAL;
            }
        } else if(_state == s_3) {
            _b008.decreaseCounter();
            if(_b008.counterLargeZero()==true) {
                scheduler.restartTimer(_b007.getDelay(), "t2", this);
                java.lang.String _b007_o3 = _b007.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b007_o3);
                    scheduler.sendToSession(this, _sessionID, "_c1_TaxiClient_init", signalData, no.ntnu.item.taxisystemgui.taxiclient.TaxiClient.getAlias(_b007_o3));
                return CONSUME_SIGNAL;
            } else {
                _state = s_1;
                return CONSUME_SIGNAL;
            }
        }
        return DISCARD_SIGNAL;
    }

    private int handleTimer_t1(){
        if(_state == s_2) {
            _b008.decreaseCounter();
            if(_b008.counterLargeZero()==true) {
                scheduler.startTimer(_b007.getDelay(), "t2", this);
                java.lang.String _b007_o3 = _b007.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b007_o3);
                    scheduler.sendToSession(this, _sessionID, "_c1_TaxiClient_init", signalData, no.ntnu.item.taxisystemgui.taxiclient.TaxiClient.getAlias(_b007_o3));
                return CONSUME_SIGNAL;
            } else {
                _state = s_4;
                return CONSUME_SIGNAL;
            }
        } else if(_state == s_3) {
            _b008.decreaseCounter();
            if(_b008.counterLargeZero()==true) {
                scheduler.startTimer(_b007.getDelay(), "t2", this);
                java.lang.String _b007_o3 = _b007.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b007_o3);
                    scheduler.sendToSession(this, _sessionID, "_c1_TaxiClient_init", signalData, no.ntnu.item.taxisystemgui.taxiclient.TaxiClient.getAlias(_b007_o3));
                return CONSUME_SIGNAL;
            } else {
                _state = s_1;
                return CONSUME_SIGNAL;
            }
        }
        return DISCARD_SIGNAL;
    }

    private int handleTimer_t0(){
        if(_state == s_2) {
            _b006.decreaseCounter();
            if(_b006.counterLargeZero()==true) {
                scheduler.startTimer(_b005.getDelay(), "t3", this);
                java.lang.String _b005_o3 = _b005.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b005_o3);
                    scheduler.sendToSession(this, _sessionID, "_c0_UserClient_init", signalData);
                return CONSUME_SIGNAL;
            } else {
                _state = s_3;
                return CONSUME_SIGNAL;
            }
        } else if(_state == s_4) {
            _b006.decreaseCounter();
            if(_b006.counterLargeZero()==true) {
                scheduler.startTimer(_b005.getDelay(), "t3", this);
                java.lang.String _b005_o3 = _b005.createID();
                String _sessionID = scheduler.createFreshSessionID();
                    java.util.Map<String, Object> signalData = new java.util.HashMap<String,Object>();
                    signalData.put("_data", _b005_o3);
                    scheduler.sendToSession(this, _sessionID, "_c0_UserClient_init", signalData);
                return CONSUME_SIGNAL;
            } else {
                _state = s_1;
                return CONSUME_SIGNAL;
            }
        }
        return DISCARD_SIGNAL;
    }

    public java.lang.String getPartName(){
        return null;
    }

    public java.lang.String getId(){
        return this._stateMachineID;
    }
}
